package io.renren.modules.archive.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.file.FileDownloadUtil;
import io.renren.common.utils.ExportUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.archive.entity.ArchiveInfoEntity;
import io.renren.modules.archive.service.ArchiveInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 档案信息表
 *
 * @author lizeliang
 * @email 2749877017@qq.com
 * @date 2023-07-27 16:41:13
 */
@RestController
@RequestMapping("archive/archiveinfo")
public class ArchiveInfoController {
    @Autowired
    private ArchiveInfoService archiveInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("archive:archiveinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = archiveInfoService.queryPage(params);

        if(page.getList().size()==0){
            Page pag=(Page)params.get("page");

            if(pag.getCurrent()>1){
                long i=pag.getCurrent()-1;
                params.put("page",Long.toString(i));
                page = archiveInfoService.queryPage(params);
            }
        }


        return R.ok().put("page", page);
    }


    /**
     * 档案查询
     */
    @RequestMapping("/query")
//    @RequiresPermissions("archive:archiveinfo:query")
    public R query(@RequestParam Map<String, Object> params){
        PageUtils page = archiveInfoService.conditionQueryPage(params);

        if(page.getList().size()==0){
            Page pag=(Page)params.get("page");

            if(pag.getCurrent()>1){
                long i=pag.getCurrent()-1;
                params.put("page",Long.toString(i));
                page = archiveInfoService.queryPage(params);
            }
        }


        return R.ok().put("page", page);
    }











    /**
     * 信息
     */
    @RequestMapping("/info/{archiveId}")
//    @RequiresPermissions("archive:archiveinfo:info")
    public R info(@PathVariable("archiveId") Long archiveId){
		ArchiveInfoEntity archiveInfo = archiveInfoService.getById(archiveId);

        return R.ok().put("archiveInfo", archiveInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("archive:archiveinfo:save")
    public R save(@RequestBody ArchiveInfoEntity archiveInfo){
		archiveInfoService.save(archiveInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("archive:archiveinfo:update")
    public R update(@RequestBody ArchiveInfoEntity archiveInfo){
		archiveInfoService.updateById(archiveInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("archive:archiveinfo:delete")
    public R delete(@RequestBody Long[] archiveIds){
		archiveInfoService.removeByIds(Arrays.asList(archiveIds));

        return R.ok();
    }


    /**
     * 文件挂载
     */
    @RequestMapping("/saveWjgz")
//    @RequiresPermissions("archive:archiveinfo:saveWjgz")
    public R saveWjgz(@RequestBody ArchiveInfoEntity archiveInfoEntity) {
        UpdateWrapper<ArchiveInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("archive_id", archiveInfoEntity.getArchiveId()).set("attach_id", archiveInfoEntity.getAttachId());
        archiveInfoService.update(updateWrapper);

        return R.ok();
    }

    /**
     * 导出
     */
    @RequestMapping("/exportWd")
//    @RequiresPermissions("archive:archiveinfo:exportWd")
    public void exportWd(HttpServletRequest request, HttpServletResponse response) {

        List<ArchiveInfoEntity> list=archiveInfoService.list();
        Map<String,String> headerName=new LinkedHashMap<>();
        headerName.put("附件","attachId");
        headerName.put("年度","nd");
        headerName.put("题名","title");
        headerName.put("责任者","author");
        headerName.put("日期","rq");
        headerName.put("文号","wh");
        headerName.put("全宗号","qzh");
        headerName.put("档号","dh");
        headerName.put("机构/问题","jg");
        headerName.put("保管期限","bgqx");
        headerName.put("件号","unitno");
        headerName.put("档案门类代码","damldm");
        headerName.put("拟稿单位","ngdw");
        headerName.put("文种","wz");
        headerName.put("拟稿人","ngr");
        headerName.put("备考说明","bksm");
        headerName.put("目录号","mlh");
        headerName.put("正本库位号","zbkwh");
        headerName.put("盒厚度","hhd");
        headerName.put("档案类型","dalx");
        headerName.put("页数","ys");
        headerName.put("密级","mj");
        headerName.put("盒号","hh");
        headerName.put("分类号","flh");
        headerName.put("备注","bz");
        headerName.put("著录层级","zlcj");
        headerName.put("范围和提要","fwhty");
        headerName.put("销毁情况","xhqk");
        headerName.put("开放标识","kfbs");
        headerName.put("公开属性","gksx");
        headerName.put("关键词","gjc");

        try {
            ExportUtils.exportExcel("档案信息导出", list,headerName, ArchiveInfoEntity.class, response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 下载模板
     */

    @RequestMapping("/template")
//    @RequiresPermissions("archive:archiveinfo:template")
    public void template(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "";
        InputStream in = null;
        fileName = "模板.xlsx";
        in = this.getClass().getClassLoader().getResourceAsStream("static/template/" + fileName);
        try {
            FileDownloadUtil.fileDownload(in,fileName,response);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }








    /**
     * 导入
     */
    @RequestMapping("/importWd")
//    @RequiresPermissions("archive:archiveinfo:importWd")
    public R importExcel(@RequestParam("file")MultipartFile excel){
        Map<String,String> headerName=new LinkedHashMap<>();
        headerName.put("附件","attachId");
        headerName.put("年度","nd");
        headerName.put("题名","title");
        headerName.put("责任者","author");
        headerName.put("日期","rq");
        headerName.put("文号","wh");
        headerName.put("全宗号","qzh");
        headerName.put("档号","dh");
        headerName.put("机构/问题","jg");
        headerName.put("保管期限","bgqx");
        headerName.put("件号","unitno");
        headerName.put("档案门类代码","damldm");
        headerName.put("拟稿单位","ngdw");
        headerName.put("文种","wz");
        headerName.put("拟稿人","ngr");
        headerName.put("备考说明","bksm");
        headerName.put("目录号","mlh");
        headerName.put("正本库位号","zbkwh");
        headerName.put("盒厚度","hhd");
        headerName.put("档案类型","dalx");
        headerName.put("页数","ys");
        headerName.put("密级","mj");
        headerName.put("盒号","hh");
        headerName.put("分类号","flh");
        headerName.put("备注","bz");
        headerName.put("著录层级","zlcj");
        headerName.put("范围和提要","fwhty");
        headerName.put("销毁情况","xhqk");
        headerName.put("开放标识","kfbs");
        headerName.put("公开属性","gksx");
        headerName.put("关键词","gjc");
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(excel.getInputStream());
         R r=ExportUtils.importExcel(workbook,ArchiveInfoEntity.class,headerName);
          int code= (int) r.get("code");
         if(code==500){
             return r;
         }else{
           List<ArchiveInfoEntity> list= (List<ArchiveInfoEntity>) r.get("list");
             for (ArchiveInfoEntity ai :list) {
                 archiveInfoService.save(ai);
             }

         }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

}
